package xuhaijia.lanou3g.autohome.findcar.screen;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.NetTool;

/**
 * Created by 7 on 16/8/24.
 */
public class ScreenAdapter extends BaseAdapter{
    Context context;
    ScreenBean screenBean;
    NetTool mNetTool = new NetTool();

    public ScreenAdapter(Context context) {
        this.context = context;
    }

    public void setScreenBean(ScreenBean screenBean) {
        this.screenBean = screenBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (screenBean.getResult().getSeries() == null) {
            return 0;
        }
        return screenBean.getResult().getSeries().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_screen , null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        mNetTool.getImage(screenBean.getResult().getSeries().get(position).getThumburl() , viewHolder.iv);
        viewHolder.titleTv.setText(screenBean.getResult().getSeries().get(position).getSeriesname());
        viewHolder.priceTv.setText(screenBean.getResult().getSeries().get(position).getPricerange());
        String rank = screenBean.getResult().getSeries().get(position).getCornericon();
        if (rank.equals("1")) {
            viewHolder.rankIv.setVisibility(View.VISIBLE);
            viewHolder.rankIv.setBackgroundResource(R.mipmap.rankfirst);
        } else if (rank.equals("2")) {
            viewHolder.rankIv.setVisibility(View.VISIBLE);
            viewHolder.rankIv.setBackgroundResource(R.mipmap.ranksecond);
        } else if (rank.equals("3")) {
            viewHolder.rankIv.setVisibility(View.VISIBLE);
            viewHolder.rankIv.setBackgroundResource(R.mipmap.rankthird);
        } else {
            viewHolder.rankIv.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView iv;
        TextView titleTv;
        TextView priceTv;
        ImageView rankIv;
        public ViewHolder(View view) {
            iv = (ImageView) view.findViewById(R.id.item_screen_iv);
            titleTv = (TextView) view.findViewById(R.id.item_screen_title);
            priceTv = (TextView) view.findViewById(R.id.item_screen_price);
            rankIv = (ImageView) view.findViewById(R.id.item_screen_rank);
        }
    }
}
